import java.util.Arrays;
import java.util.Scanner;

public class NGuessNumber {
    public int[] setPlayerAnswer(){
        Scanner sca = new Scanner(System.in);
        int[] playerAnswer = new int[4];
        for (int i = 0; i < 4; i++) {
            playerAnswer[i] = sca.nextInt();
        }
        return playerAnswer;
    }

    public int[] setAnswer(){
        int[] answer = new int[4];
        for (int i = 0; i < 4; i++) {
            answer[i] = (int)(Math.random()*10);
        }
        return answer;
    }

    public int[] screen(int[] playerAnswer,int[] answer){
        int a = 0;
        int b = 0;
        int[] screen = new int[2];
        for (int i = 0; i < 4; i++) {
            if (playerAnswer[i] == answer[i]){
                a++;
            }
            if (Arrays.asList(answer).contains(playerAnswer[i])){
                b++;
            }
        }
        screen[0] = a;
        screen[1] = b;
        return screen;
    }

    public void app(){
        System.out.println("游戏玩法：根据提示猜对4个数字和他们的顺序即可获胜！");
        System.out.println("请输入4个0~9之间的数字：(每输入一个数字点击一次enter键)");
        NGuessNumber nGuessNumber = new NGuessNumber();

        int[] answer = nGuessNumber.setAnswer();
        for (int i = 0; i < 4; i++) {
            System.out.println(answer[i]);
        }
        int[] playerAnswer = nGuessNumber.setPlayerAnswer();
        int[] screen = nGuessNumber.screen(playerAnswer,answer);

        if (screen[0] != 4){
            while (true){
                System.out.println(screen[0]+" A "+screen[1]+" B");
                System.out.println("A表示纯在且位置正确的数字个数；B表示存在但位置不正确的数字个数！");
                System.out.println("eg. 1A2B,表示猜到的4个数字中有3个是对的，1个位置对，2个位置不对。");
                if (screen[0] != 4){
                    System.out.println("请输入4个0~9之间的数字：(每输入一个数字点击一次enter键)");

                    playerAnswer = nGuessNumber.setPlayerAnswer();
                    screen = nGuessNumber.screen(playerAnswer,answer);
                }else {
                    System.out.println("恭喜你猜对啦！再玩一次吗？（y/n）");
                    Scanner sca = new Scanner(System.in);
                    String isAgain = sca.nextLine();
                    if (isAgain.equals("y")){
                        app();
                    }else {
                        break;
                    }
                }
            }
        }else {
            System.out.println("恭喜你猜对啦！再玩一次吗？（y/n）");
            Scanner sca = new Scanner(System.in);
            String isAgain = sca.nextLine();
            if (isAgain.equals("y")){
                app();
            }
        }
    }

    public static void main(String[] args) {
        NGuessNumber nGuessNumber = new NGuessNumber();
        nGuessNumber.app();
    }
}
